package gestion.camping.vue;

import gestion.camping.controleur.FraisReservationController;
import gestion.camping.dto.FraisReservation;
import gestion.camping.utils.MessagesUtil;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ViewFraisReservationListe extends JPanel implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = -1923064145354238975L;
	
	private TableFraisReservation table;

	private List<FraisReservation> listFraisReservation;
	private FraisReservationController controller;

	private JButton btnValider;
	private JButton btnAjouter;

	/**
	 * Create the panel.
	 */
	public ViewFraisReservationListe(FraisReservationController fraisReservationController) {
	
		this.controller = fraisReservationController;
		
		this.listFraisReservation = fraisReservationController.getListFraisReservation();
		
		initCompoment();
	}
	
	public void initCompoment(){
	
		setBounds(0, 25, 1150, 700);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.15, 0.2, 0.15, 0.2, 0.15, 0.15};
		gridBagLayout.rowWeights = new double[]{0.1, 0.0, 0.0, 0.0, 0.0, 0.5, 0.1};
		setLayout(gridBagLayout);
		
		table = new TableFraisReservation();
		
		GridBagConstraints gbc_table = new GridBagConstraints();
		gbc_table.gridwidth = 4;
		gbc_table.insets = new Insets(0, 0, 5, 5);
		gbc_table.fill = GridBagConstraints.BOTH;
		gbc_table.gridx = 1;
		gbc_table.gridy = 5;
		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, gbc_table);
		
		btnAjouter = new JButton(MessagesUtil.formatMessage("fraisReservation.ajout.title"));
		btnAjouter.addActionListener(this);
		GridBagConstraints gbc_btnAjouter = new GridBagConstraints();
		gbc_btnAjouter.anchor = GridBagConstraints.EAST;
		gbc_btnAjouter.insets = new Insets(0, 0, 0, 5);
		gbc_btnAjouter.gridx = 3;
		gbc_btnAjouter.gridy = 6;
		add(btnAjouter, gbc_btnAjouter);
		
		btnValider = new JButton(MessagesUtil.formatMessage("fraisReservation.liste.btn.modifier"));
		btnValider.addActionListener(this);
		GridBagConstraints gbc_btnValider = new GridBagConstraints();
		gbc_btnValider.anchor = GridBagConstraints.WEST;
		gbc_btnValider.insets = new Insets(0, 0, 0, 5);
		gbc_btnValider.gridx = 4;
		gbc_btnValider.gridy = 6;
		add(btnValider, gbc_btnValider);
		
	}

	public List<FraisReservation> getListFraisReservation() {
		return listFraisReservation;
	}

	public void setListFraisReservation(List<FraisReservation> listFraisReservation) {
		this.listFraisReservation = listFraisReservation;
		
		update();
	}
	
	private void update(){
		int indexElementSelected = table.getSelectedRow();
		
		table.setModel(new TableFraisReservationModel(listFraisReservation, table.getColumnNames()));
		table.setShowGrid(true);
		table.setVisible(true);
		
		this.repaint();
		
		if(indexElementSelected >= 0){
			table.setRowSelectionInterval(indexElementSelected, indexElementSelected);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnValider){
			FraisReservation selectedFraisReservation = listFraisReservation.get(this.table.getSelectedRow());			
			
			this.controller.modifier(selectedFraisReservation);
		}else if(e.getSource() == btnAjouter){
			this.controller.ajouter();
		}
		
	}
	
	public FraisReservation getSelectedFraisReservation(){	
		return table.getSelectedRow() < 0 ? null : listFraisReservation.get(table.getSelectedRow());
	}

}
